package edu.gatech.ealf.magiclenseplaf;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.TextUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;
import javax.swing.text.Position.Bias;

public class MLTextUI extends TextUI implements MouseListener {

    
    
    private ZoomHelper zoomer = null;
    
    public MLTextUI() {
        super();
    }

    public static ComponentUI createUI(JComponent c) {
        return new MLTextUI();
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        MagicLenseLookAndFeel.getInstance().registerComponentInstallation(this, c);
        zoomer = new ZoomHelper(c);
        c.addMouseListener(this);
    }

    public void paint(Graphics g, JComponent c) {
    }

    @Override
    public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
        MagicLenseLookAndFeel.getInstance().registerComponentUninstallation(this, c);
        c.removeMouseListener(this);
        if(zoomer != null){
            zoomer.destroy();
        }
        zoomer = null;
    }

    @Override
    public void update(Graphics g, JComponent c) {
        paint(g, c);
    }

    public void mouseClicked(MouseEvent e) {
        if(e.getButton()== MouseEvent.BUTTON2){
            zoomer.toggleZoomable();
        }
    }


    public void mouseEntered(MouseEvent e) {
        //System.out.println("MLTextUI.mouseEntered");
        zoomer.zoom();
        
    }

    public void mouseExited(MouseEvent e) {
        //System.out.println("MLTextUI.mouseExited");
        boolean hasFocus = zoomer.curComponent.hasFocus();
        zoomer.unZoom();
        if(hasFocus){
            zoomer.curComponent.requestFocusInWindow();
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void damageRange(JTextComponent t, int p0, int p1) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void damageRange(JTextComponent t, int p0, int p1, Bias firstBias, Bias secondBias) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public EditorKit getEditorKit(JTextComponent t) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getNextVisualPositionFrom(JTextComponent t, int pos, Bias b, int direction, Bias[] biasRet) throws BadLocationException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getRootView(JTextComponent t) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Rectangle modelToView(JTextComponent t, int pos) throws BadLocationException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Rectangle modelToView(JTextComponent t, int pos, Bias bias) throws BadLocationException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int viewToModel(JTextComponent t, Point pt) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int viewToModel(JTextComponent t, Point pt, Bias[] biasReturn) {
        // TODO Auto-generated method stub
        return 0;
    }
}

